//
//  CSObjectUtils.swift
//  Cycling
//
//  Created by CSS on 3/29/23.
//

import UIKit
import MapboxDirections
import MapboxCoreNavigation
import MapboxNavigation
import CoreLocation

class CSMapManager: NSObject {

    @objc class func requestRoutes(withFromLocation fromLocation: CLLocationCoordinate2D, destinationLocation: CLLocationCoordinate2D, presentedVC: UIViewController) {
        let origin = Waypoint(coordinate: fromLocation)
        let destination = Waypoint(coordinate: destinationLocation)
        
        let routeOptions = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .cycling)
        
        Directions.shared.calculate(routeOptions) { session, result in
            switch result {
            case .failure(let error):
                print(error.localizedDescription)
            case .success(let response):
                // Pass the first generated route to the the NavigationViewController
//                guard let firstRoute = response.routes?.first else {
//                    return
//                }
                let navigationService = MapboxNavigationService(indexedRouteResponse: .init(routeResponse: response, routeIndex: 0), credentials: NavigationSettings.shared.directions.credentials, simulating: .onPoorGPS)
                let navigationOptions = NavigationOptions(navigationService: navigationService)
                let viewController = NavigationViewController(for: .init(routeResponse: response, routeIndex: 0), navigationOptions: navigationOptions)
                viewController.modalPresentationStyle = .fullScreen
                presentedVC.present(viewController, animated: true, completion: nil)
            }
        }
        
    }
    
}
